package com.frxuexi.park2302.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding,NewsIntent,NewsViewModel>() {
    override val binding: ActivityNewsBinding by lazy { ActivityNewsBinding.inflate(layoutInflater) }
    override val vm: NewsViewModel by viewModels<NewsViewModel>()
    val newsAdapter by lazy { NewsAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                newsAdapter.apply {
                                    newsAdapter.data.clear()
                                    val list = it.data as Collection<NewsBeanItem>
                                    addData(list.filter { it.published })
                                    notifyDataSetChanged()
                                }
                            }
                            RequestType.UPDATE->{
                                vm.sendIntent(NewsIntent.getNews)
                            }

                            else -> {}
                        }

                    }
                    is UiState.onError->{
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.newBack.setOnClickListener {
            finish()
        }
        binding.newsAdd.setOnClickListener {
            ARouter.getInstance().build("/app/NewsAddActivity").navigation()
        }
        binding.newsRv.apply {
            adapter=newsAdapter
            layoutManager=LinearLayoutManager(this@NewsActivity)
        }


        binding.newSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s!!.isNotEmpty()) {
                    vm.sendIntent(NewsIntent.getNewsByKey(s.toString()))
                }else{
                    vm.sendIntent(NewsIntent.getNews)
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        newsAdapter.setOnItemLongClickListener { adapter, view, position ->
            vm.sendIntent(NewsIntent.revokeNews(newsAdapter.data.get(position).id!!))
            return@setOnItemLongClickListener true
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(NewsIntent.getNews)
    }

}